In [1]:
# ! pip install plotly==5.6.0
# ! pip install "notebook>=5.3" "ipywidgets>=7.5"
In [2]:
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
from plotly import graph_objs as go
In [3]:
## load in the hierarchy information
url = "https://raw.githubusercontent.com/bcaffo/MRIcloudT1volumetrics/master/inst/extdata/multilevel_lookup_table.txt"
multilevel_lookup = pd.read_csv(url, sep = "\t").drop(['Level5'], axis = 1)
multilevel_lookup = multilevel_lookup.rename(columns = {
    "modify"   : "roi", 
    "modify.1" : "level4",
    "modify.2" : "level3", 
    "modify.3" : "level2",
    "modify.4" : "level1"})
multilevel_lookup = multilevel_lookup[['roi', 'level4', 'level3', 'level2', 'level1']]
multilevel_lookup.head()
Out[3]:
roi level4 level3 level2 level1
0 SFG_L SFG_L Frontal_L CerebralCortex_L Telencephalon_L
1 SFG_R SFG_R Frontal_R CerebralCortex_R Telencephalon_R
2 SFG_PFC_L SFG_L Frontal_L CerebralCortex_L Telencephalon_L
3 SFG_PFC_R SFG_R Frontal_R CerebralCortex_R Telencephalon_R
4 SFG_pole_L SFG_L Frontal_L CerebralCortex_L Telencephalon_L
In [4]:
## Now load in the subject data
id = 127
subjectData = pd.read_csv("./data/kirby21AllLevels.csv", header = 0)
subjectData = subjectData.loc[(subjectData.type == 1) & (subjectData.level == 5) & (subjectData.id == id)]
subjectData = subjectData[['roi', 'volume']]
## Merge the subject data with the multilevel data
subjectData = pd.merge(subjectData, multilevel_lookup, on = "roi")
subjectData = subjectData.assign(level0 = "ICV")
subjectData = subjectData.assign(comp = subjectData.volume / np.sum(subjectData.volume))
subjectData.head()
Out[4]:
roi volume level4 level3 level2 level1 level0 comp
0 SFG_L 12926 SFG_L Frontal_L CerebralCortex_L Telencephalon_L ICV 0.009350
1 SFG_R 10050 SFG_R Frontal_R CerebralCortex_R Telencephalon_R ICV 0.007270
2 SFG_PFC_L 12783 SFG_L Frontal_L CerebralCortex_L Telencephalon_L ICV 0.009247
3 SFG_PFC_R 11507 SFG_R Frontal_R CerebralCortex_R Telencephalon_R ICV 0.008324
4 SFG_pole_L 3078 SFG_L Frontal_L CerebralCortex_L Telencephalon_L ICV 0.002227
In [5]:
l1 = subjectData.groupby(["level1", "level0"]).sum().reset_index()
l1 = l1.rename(columns = {"level1": "target", "level0": "source"}).drop(["volume"], axis = 1)
l1.head()
Out[5]:
target source comp
0 CSF ICV 0.079417
1 Diencephalon_L ICV 0.008548
2 Diencephalon_R ICV 0.008362
3 Mesencephalon ICV 0.007430
4 Metencephalon ICV 0.115313
In [6]:
l2 = subjectData.groupby(["level2", "level1"]).sum().reset_index()
l2 = l2.rename(columns = {"level2": "target", "level1": "source"}).drop(["volume"], axis = 1)
l2.head()
Out[6]:
target source comp
0 BasalForebrain_L Diencephalon_L 0.003960
1 BasalForebrain_R Diencephalon_R 0.003753
2 CerebralCortex_L Telencephalon_L 0.200361
3 CerebralCortex_R Telencephalon_R 0.204623
4 CerebralNucli_L Telencephalon_L 0.008956
In [7]:
rois = pd.concat([subjectData["level0"], subjectData["level1"], subjectData["level2"]]).unique().tolist()

sankey_dat = pd.concat([l1, l2])

sankey_dat["target_idx"] = [rois.index(x) for x in sankey_dat["target"]]
sankey_dat["source_idx"] = [rois.index(x) for x in sankey_dat["source"]]

sankey_dat.head()
Out[7]:
target source comp target_idx source_idx
0 CSF ICV 0.079417 8 0
1 Diencephalon_L ICV 0.008548 3 0
2 Diencephalon_R ICV 0.008362 4 0
3 Mesencephalon ICV 0.007430 5 0
4 Metencephalon ICV 0.115313 6 0
In [8]:
fig = go.Figure(data=[go.Sankey(
    node = dict(
      pad = 15,
      thickness = 40,
      line = dict(color = "black", width = 0.5),
      label = rois,
      color = "blue"
    ),
    link = dict(
      source = sankey_dat["source_idx"], # indices correspond to labels, eg A1, A2, A1, B1, ...
      target = sankey_dat["target_idx"],
      value = sankey_dat["comp"]
  ))])

fig.update_layout(title_text="Volume", height = 800, font_size=10)
fig.show()